home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / TECHNICA / AUTOCAD / H108.ZIP / MM10.ZIP / MMS.LSP < prev    next >
Lisp/Scheme  |  1991-08-01  |  11KB  |  281 lines

  1. ;;
  2. ;; 
  3. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4. ;;           AutoCAD tm  Macro Maker  V 1.00
  5. ;; Author: SCOTT HARES   1108 Kelez Dr.  San Jose CA, 95120
  6. ;;                Voice: 408-927-6337
  7. ;;            CompuServe tm  ID 73730,1643
  8. ;;           Copyright (C) 1991 Scott Hares
  9. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  10. ;;
  11. ;; To manage macros a global list called key_list is used. Key_list stores 
  12. ;; any number of dotted pairs which define a macro. These dotted pairs 
  13. ;; (referred to as a key list) hold the keys ascii code on the left, and the 
  14. ;; keys macro on the right. The structure of this arrangement can be described 
  15. ;; as a list (key_list) of key lists (dotted pairs). A typical example of this 
  16. ;; might look like: (("0;30" . "alt-a")("0;48" . "alt-b")("0;46" . "alt-c"))
  17. ;; Three macros are defined at this point, the will echo to the screen "alt-a" 
  18. ;; if alt-a is hit (same for alt-b and alt-c).
  19. ;;
  20.  
  21. (defun c:mm( / does_exist ESC RETURN BACKSP table string *error* key
  22.                table old_rec rec_key flag)
  23. (textscr)
  24.  
  25. ;;******* error handler ************
  26. (defun *error*(msg)
  27. (mmlll)
  28. (terpri)
  29. (princ (strcat "ERROR: " msg))
  30. (princ)
  31. )
  32.  
  33. ;;**************************************
  34. ;; check to see if the key pressed has already been assigned a macro. This
  35. ;; is done so that a key list will not be added into key_list more than once.
  36. ;; If this code finds that a key has already been assigned a macro (and a key
  37. ;; list for it) in key_list then the flag returned will tell MM to substitute 
  38. ;; the new key list for the old key list rather than adding key list to 
  39. ;; key_list
  40. ;;
  41. ;; Macros have been suspended at this point so keys codes will reflect there 
  42. ;; correct ascii value instead of reflecting the keys macro. 
  43. ;;
  44. ;; accepts the key code entered by user as a string
  45. ;; returns flag integer
  46. (defun does_exist( exist / x flag )
  47. (setq flag 0)                   ;; set flag to zero 
  48. (foreach x key_list             ;; check each key assignment list in key_list
  49.     (if (= exist (car x))       ;; to see if the key has already been defined
  50.        (progn
  51.           (setq flag 1)         ;; turn on flag if key code is found in key_list
  52.           (setq old_rec x)      ;; and save old key list for substitution later
  53.        );;progn                 ;; on in program
  54.     );;if
  55. );;foreach
  56. (eval  flag)                    ;; return either 0 for new macro assignment or
  57. );;defun                        ;; 1 for macro redefinition
  58.  
  59. ;;******* MAIN *******
  60.  
  61. ;;initialize variables and constants
  62. (setq key       nil
  63.       string    "\e["
  64.       table     ""
  65.       RETURN    13
  66.       BACKSP    8
  67.       ESC       27
  68. )
  69.  
  70. ;;un-init current macros so that macros will not run during definition 
  71. (mmu)
  72.  
  73. ;;get key to record to and display prompt for macro data. Assign the key 
  74. ;;value to key. Macro data is collect later 
  75. (princ               "Terminate macro record with ESCAPE key")(terpri)
  76. (princ               "Press the key you wish to assign macro to: ") 
  77. (setq rec_key (get_key)) (terpri) 
  78. (princ "Enter the keystrokes to be recorded to macro: ") (terpri)
  79.  
  80.  
  81. ;;if the user hit an extended key like ALT-1, then some formatting must be
  82. ;;done so the ansi driver knows what key combo was pressed.
  83.    (if (> rec_key 125)
  84.       (setq rec_key (strcat "0;" (itoa (- rec_key 128)) )) 
  85.       (setq rec_key (itoa rec_key))
  86.    );;if
  87.  
  88. ;;check if key has been defined already, and set flag for list substitute 
  89. ;;rather than list addition
  90. (setq flag (does_exist rec_key))
  91.  
  92. ;;required because while statement does not check to see
  93. ;;if key equals ESC until after key is processed
  94. (setq key (get_key)) 
  95.  
  96. ;;collect key strokes to be defined to a key until ESCape key is hit
  97. (while (/= key ESC)
  98.    (cond
  99.      ((= key RETURN)                    ;;return key presents problems
  100.         (setq table (strcat table " ")) ;;these two lines substitute a space
  101.         (princ " ")                     ;;for carriage return, and will behave
  102.      );;cond                            ;;the same as a carriage return
  103.      ((and (= key BACKSP) (> (strlen table) 0))            ;;get rid of
  104.         (prompt "\e[D\e[K")                                ;; unwanted
  105.         (setq table (substr table 1 (1- (strlen table)) )) ;;character
  106.      );;cond        
  107.      (T
  108.         (setq table (strcat table (chr key)))  ;;if key was not ESCape, return
  109.         (princ (chr key))                      ;;or backspace, then add 
  110.      );;cond                                   ;;character to string
  111.    );;cond
  112.    (setq key (get_key))                        ;;get next character
  113. );;while
  114.  
  115. (terpri)
  116.  
  117. ;;build key_list with current key list. three options below are to start 
  118. ;;key_list if it does not exist, add a new key list to key_list, or substitute 
  119. ;;new macro for old macro if the key has already been assigned a macro
  120. (COND 
  121.   ;;adding first definition, key_list does not yet exist
  122.   ((and (= flag 0) (not key_list))
  123.       (setq key_list (list (cons rec_key table))) );;cond
  124.  
  125.   ;;adding a new definition and key_list already exists
  126.   ((and (= flag 0) key_list)
  127.       (setq key_list (append key_list (list (cons rec_key table )))) );;cond
  128.  
  129.   ;;redefining a key which has already been defined in key_list
  130.   ((and (= flag 1) key_list) 
  131.       (setq key_list (subst (cons rec_key table ) old_rec key_list))
  132.       (setq old_rec nil) );;cond
  133. );;COND
  134.  
  135. ;;format string ready for key re-assignment
  136. (setq string (strcat string rec_key ";'"  table "'p"))
  137. ;;perform key redefinition
  138. (prompt string)
  139.  
  140. ;;reload list that was un-init before redefinition was run
  141. (mmlll)
  142. ;;exit program quietly
  143. (princ)
  144.  
  145. );;defun main
  146.  
  147. ;;****************************************************
  148. ;;write key_list to hard disk. User is prompted for file name, but no error 
  149. ;;checking is performed. The first line written to the file is an 
  150. ;;identification string to be checked. This string is checked when the file is
  151. ;;loaded from the hard disk. The original key code and the macro string is 
  152. ;;written to the file.
  153. (defun c:mmw ( / f string x )
  154. (terpri)
  155. ;;get file name, there is no error detection
  156. (setq string (getstring "Enter list file to write: "))
  157. (setq f (open string "w"))
  158. (write-line "MM  Macro file " f)
  159. (if f
  160.   (progn                        ;;loop through key_list writing data
  161.      (foreach x key_list        ;;to file
  162.        (progn
  163.          (write-line (car x) f)
  164.          (write-line (cdr x) f)
  165.        );;progn
  166.     );;foreach
  167.     (close f)
  168.   );;progn
  169.   ;;if error occurs opening file
  170.   (*error* (strcat "Cant open file: " string))
  171. );;if
  172. (princ)
  173. );;defun
  174.  
  175. ;;*****************************************************************
  176. ;;these short programs allow users access to subroutines used by MM
  177. ;;disable macros list from command line
  178. (defun c:mmul()(mmu)(terpri)(princ "Macros unloaded")(princ))
  179. ;;reload macro list from command line
  180. (defun c:mmll()(mmlll)(terpri)(princ "Macros reloaded")(princ))
  181.  
  182. ;;******************************************************************
  183. ;;load list from hard disk. If file does not exist then MMLF will abort
  184. ;;with an error message. If the file does not have the identification string 
  185. ;;then the program assumes that the file is not a valid macro file and will 
  186. ;;abort. The identification string is inserted when the file was written.
  187. (defun c:mmlf( / x1 x2 string f)
  188. (terpri)
  189. (setq key_list nil)                                     ;;clear key_list
  190. (setq string (getstring "Enter list file to load: "))   ;;get file name
  191. (setq f (open string "r"))                              ;;open file
  192. (if f
  193.   (progn
  194.     (if (= (read-line f) "MM  Macro file ")             ;;check if file is 
  195.       (progn                                            ;;valid macro file
  196.         ;if file good                                   ;;if file is valid
  197.         (while (setq x1 (read-line f))                  ;;then start reading
  198.           (setq x2 (read-line f))                       ;;lines and building
  199.             (if key_list                                ;;key lists
  200.                ;;if key_list is empty then create it with first key list
  201.                (setq key_list (append key_list (list (cons x1 x2))))
  202.                ;;if key list is not empty, then add next key list
  203.                (setq key_list (list (cons x1 x2)))
  204.             );if
  205.         );while
  206.       );progn
  207.       ;;if identification line did not match then abort
  208.       (*error* (strcat string " is not a valid macro file"))
  209.     );if is file valid macro file
  210.     (close f)
  211.   );progn
  212.   ;;if file did not exist then abort
  213.   (*error* (strcat string " is not a valid macro file"))
  214. );if file exists
  215.                  
  216. (mmlll)   ;;now that key_list has been built, macro redefinition 
  217.           ;;still must be performed to redefine keys
  218. (princ)   ;;exit program quietly
  219. )
  220. ;;*****************************************************
  221. ;;undo macro redefinitions and assign keys their correct value
  222. (defun mmu( / x key )
  223. (foreach x key_list
  224.   (progn
  225.     (setq key (car x))
  226.     (prompt (strcat "\e[" key ";" key "p"))
  227.   );;progn
  228. )
  229. )
  230. ;;*****************************************************
  231. ;;initialize macros from key_list
  232. (defun mmlll( / x string)
  233. (textscr)
  234. (if key_list
  235.   (foreach x key_list
  236.     (progn
  237.       (setq string (strcat "\e[" (car x) ";'" (cdr x) "'p")) ;;build string
  238.       (prompt string)                                        ;;execute
  239.     );;progn
  240.   );;foreach
  241. );;if 
  242. );;defun
  243.  
  244. ;;******************************************************
  245. ;; regular get key function ****
  246. ;;returns character integer
  247. (defun get_key( / code key )
  248. (while (/= code 2)             
  249.    (setq code (grread)) 
  250.    (setq key (car (cdr code)))
  251.    (setq code (car code))
  252. );;while
  253. (eval key)
  254. );;defun
  255.  
  256.  
  257. (princ)
  258. (terpri)
  259. (princ  " Macro Maker Ver 1.00   Copyright (c) 1991 Scott Hares.  Invoke with MM")  
  260. (terpri)
  261. (princ  " Please send $5.00 donation to Scott Hares 1108 Kelez Dr. San Jose Ca, 95120")
  262. (terpri)
  263. ;;-------------------------------------------------------------------------
  264. ;;because the ansi driver is used to assign macros to keys, the macros must be 
  265. ;;unloaded every time the drawing editor is left. The ansi driver runs at a 
  266. ;;much lower level than the application running. This means that key 
  267. ;;redefinitions will still be in effect until the system is rebooted or the 
  268. ;;macros are explicitly un-defined. This can obviously interfere with other 
  269. ;;applications the user might want to run. Macro unloading is done with the 
  270. ;;key_list which is a global variable. This global variable however is lost 
  271. ;;when the drawing editor is exited. Therefore the macros must be unloaded 
  272. ;;every time the editor is exited. The following lines redefine the standard 
  273. ;;Quit and End commands to include a function call to the MMUL function.
  274. (setvar "CMDECHO" 0)
  275. (command "undefine" "quit")
  276. (command "undefine" "end")
  277. (defun c:quit()(setvar "cmdecho" 0)(textscr)(mmu)(princ)(command ".quit"))
  278. (defun c:end() (setvar "cmdecho" 0)(textscr)(mmu)(princ)(command ".end"))
  279. (setvar "CMDECHO" 1)
  280. (prin1)
  281.